﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using WallpaperCrawler;
using WallpaperDemo.Models;

namespace WallpaperDemo.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class CrawlController : ControllerBase
    {
        private readonly WallpapersContext _context;
        private CrawlerService crawlerService;
        Crawler myCrawler = new Crawler();
        public List<UrlInfo> Urls { get; set; } = new List<UrlInfo>();
        private int count = 0;

        public CrawlController(WallpapersContext context)
        {
            _context = context;
            crawlerService = new CrawlerService(context);
        }

        //GET: api/Crawl/start
        [HttpGet("start")]
        public ActionResult<List<string>> Crawl()
        {
            crawlerService.start();
            return new List<string>();
        }

        //GET: api/Crawl/download/1
        [HttpGet("download/{seq}")]
        public ActionResult<List<UrlInfo>> DownloadImgsFromSeq(int seq)
        {
            return crawlerService.PraseOnePage(seq);
        }

        //GET: api/Crawl/search/forest
        [HttpGet("search/{keyword}")]
        public ActionResult<List<UrlInfo>> GetKey(string keyword)
        {
            List<UrlInfo> res = crawlerService.GetKey(keyword);
            return res;
        }

        //GET: api/Crawl/search/forest/2
        [HttpGet("search/{keyword}/{seq}")]
        public ActionResult<List<UrlInfo>> GetNext(string keyword, int seq)
        {
            List<UrlInfo> res = crawlerService.DownloadNext(keyword, seq);
            return res;
        }

        //GET: api/Crawl/any
        [HttpGet("any")]   //大失败！！
        public ActionResult<List<UrlInfo>> GetOne()
        {
            List<UrlInfo> s = crawlerService.GetOne();
            return s;
        }
    }
}
